package com.link2room.soundwave.facade.impl;

import com.greencloud.api.dto.membercard.PointWebDto;
import com.greencloud.api.request.SaveMemberCardPayRequest;
import com.greencloud.api.request.coupon.FindCouponDetailListByCondiRequest;
import com.greencloud.api.request.coupon.FindCouponDetailListByMemberIdRequest;
import com.greencloud.api.request.membercard.*;
import com.greencloud.api.response.PmsResponse;
import com.greencloud.api.response.coupon.CouponWebResponse;
import com.greencloud.api.response.membercard.*;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.dto.result.base.MemberSessionDto;
import com.link2room.common.dto.threadlocal.ApiThreadLocal;
import com.link2room.common.exception.SysException;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.*;
import com.link2room.enums.exception.SoundwaveSysExceptionEnum;
import com.link2room.soundwave.dto.param.inside.*;
import com.link2room.soundwave.dto.result.base.PmsCardBalanceDto;
import com.link2room.soundwave.dto.result.base.PmsCouponDto;
import com.link2room.soundwave.dto.result.base.PmsMemberInfoDto;
import com.link2room.soundwave.dto.result.base.PmsPackageDto;
import com.link2room.soundwave.entity.*;
import com.link2room.soundwave.exception.SoundwaveException;
import com.link2room.soundwave.service.IGcPmsMemberService;
import com.link2room.soundwave.util.GcPmsUtil;
import greencloud.ipms2.constant.BindMemberConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.link2room.enums.exception.SoundwaveSysExceptionEnum.GCPMS_SALE_COUPON_ERROR;


/**
 * Created by VegetaKo on 2016/9/18.
 */
@Component("gcPmsMemberHandlerFacadeService")
public class GcPmsMemberHandlerFacadeServiceImpl extends AbstractPmsMemberHandlerFacadeServiceImpl {
    private Logger logger = LoggerFactory.getLogger(GcPmsMemberHandlerFacadeServiceImpl.class);
    @Resource
    private ICommonService commonService;
    @Resource
    private IGcPmsMemberService gcPmsMemberService;

    @Override
    public PmsMemberInfoDto checkPMSMember(MemberLoginInsideParamDto memberLoginParamDto) throws Exception {
        String lhotelGroupCode = memberLoginParamDto.getLhotelGroupCode();
        PmsMemberInfoDto pmsMemberInfoDto = new PmsMemberInfoDto();

        L2RIpms2Member queryLIM = new L2RIpms2Member();
        queryLIM.setL2RMemberNo(memberLoginParamDto.getL2RMemberNo());
        queryLIM.setLhotelGroupCode(lhotelGroupCode);
        QueryResult<L2RIpms2Member> l2RIpms2MemberResult = commonService.findAllEQ(L2RIpms2Member.class, queryLIM);
        L2RIpms2Member l2RIpms2Member = null;
        if (l2RIpms2MemberResult.getResultlist() != null && l2RIpms2MemberResult.getResultlist().size() > 0) {
            l2RIpms2Member = l2RIpms2MemberResult.getResultlist().get(0);
        }
        if (null == l2RIpms2Member) {
            throw new SysException(SoundwaveSysExceptionEnum.PMSMEMBER_NOT_EXIT);
        } else {
            if (StringUtil.isBlank(l2RIpms2Member.getCardNo())) {
                L2RIpms2Group l2RIpms2Group = new L2RIpms2Group();
                l2RIpms2Group.setLhotelGroupCode(memberLoginParamDto.getLhotelGroupCode());
                l2RIpms2Group = commonService.findOneEQ(L2RIpms2Group.class, l2RIpms2Group);

                //ipms2MemberLoginParamDto.setCardNo();
                this.bindMemberInfo(lhotelGroupCode,
                        l2RIpms2Group.getHotelGroupId(),
                        l2RIpms2Member.getMemberId(),
                        BindMemberConstant._bindOpenType.LINK2ROOM,
                        memberLoginParamDto.getL2RMemberNo(),
                        ""
                );
                pmsMemberInfoDto = this.getMemberInfoOpen(lhotelGroupCode,
                        l2RIpms2Group.getHotelGroupId(),
                        null,
                        memberLoginParamDto.getL2RMemberNo(),
                        BindMemberConstant._bindOpenType.LINK2ROOM
                );

                if (null != pmsMemberInfoDto) {
                    this.mergeL2RIpms2Member(l2RIpms2Member, pmsMemberInfoDto);
                    return pmsMemberInfoDto;
                }
            } else {
                ClassConverUtil.copyProperties(l2RIpms2Member, pmsMemberInfoDto);
                return pmsMemberInfoDto;
            }
        }
        return pmsMemberInfoDto;
    }

    @Override
    public PmsMemberInfoDto bindingPMSMember(MemberLoginInsideParamDto memberLoginParamDto) throws Exception {
        L2RIpms2Member querylpm = new L2RIpms2Member();
        querylpm.setLhotelGroupCode(memberLoginParamDto.getLhotelGroupCode());
        querylpm.setL2RMemberNo(memberLoginParamDto.getL2RMemberNo());
        L2RIpms2Member l2RIpms2Member = commonService.findOneEQ(L2RIpms2Member.class, querylpm);


        L2RIpms2Group l2RIpms2Group = new L2RIpms2Group();
        l2RIpms2Group.setLhotelGroupCode(memberLoginParamDto.getLhotelGroupCode());
        l2RIpms2Group = commonService.findOneEQ(L2RIpms2Group.class, l2RIpms2Group);

        PmsMemberInfoDto pmsMemberInfoDto = this.getMemberInfo(memberLoginParamDto.getLhotelGroupCode(),
                l2RIpms2Group.getHotelGroupId(),
                memberLoginParamDto.getLoginId(),
                memberLoginParamDto.getLoginPasswd());

        if (null != pmsMemberInfoDto && StringUtil.isNotBlank(pmsMemberInfoDto.getCardNo())) {
            this.bindMemberInfo(memberLoginParamDto.getLhotelGroupCode(),
                    l2RIpms2Group.getHotelGroupId(),
                    pmsMemberInfoDto.getMemberId(),
                    BindMemberConstant._bindOpenType.LINK2ROOM,
                    memberLoginParamDto.getL2RMemberNo(),
                    pmsMemberInfoDto.getCardNo());
            if (null == l2RIpms2Member) {
                this.saveL2RIpms2Member(memberLoginParamDto.getLhotelGroupCode(),
                        memberLoginParamDto.getL2RMemberNo(),
                        l2RIpms2Group.getHotelGroupId(), Long.parseLong(pmsMemberInfoDto.getPmsHotelCode()),
                        pmsMemberInfoDto);
            } else {
                Long id = l2RIpms2Member.getId();
                ClassConverUtil.copyProperties(pmsMemberInfoDto, l2RIpms2Member);
                l2RIpms2Member.setHotelGroupId(l2RIpms2Group.getHotelGroupId());
                l2RIpms2Member.setId(id);
                l2RIpms2Member.setLhotelGroupCode(l2RIpms2Group.getLhotelGroupCode());
                l2RIpms2Member.setIsHalt(DefaultValue.F);
                this.mergeL2RIpms2Member(l2RIpms2Member, pmsMemberInfoDto);
            }
            return pmsMemberInfoDto;
        } else {
            throw new SoundwaveException(SoundwaveSysExceptionEnum.PMSMEMBER_NOT_MATCH);
        }

        /*if (null == l2RIpms2Member) {
            this.saveL2RIpms2Member(memberLoginParamDto.getLhotelGroupCode(),
                    memberLoginParamDto.getL2RMemberNo(),
                    l2RIpms2Group.getHotelGroupId(),
                    pmsMemberInfoDto);
        } else {
            ClassConverUtil.copyProperties(pmsMemberInfoDto, l2RIpms2Member);
            l2RIpms2Member.setHotelGroupId(l2RIpms2Group.getHotelGroupId());
            this.mergeL2RIpms2Member(l2RIpms2Member,pmsMemberInfoDto);
        }*/

    }

    @Override
    public void unbindingPMSMember(MemberLoginInsideParamDto memberLoginParamDto) throws Exception {
        L2RIpms2Group l2RIpms2Group = new L2RIpms2Group();
        l2RIpms2Group.setLhotelGroupCode(memberLoginParamDto.getLhotelGroupCode());
        l2RIpms2Group = commonService.findOneEQ(L2RIpms2Group.class, l2RIpms2Group);

        L2RIpms2Member queryLIM = new L2RIpms2Member();
        queryLIM.setL2RMemberNo(memberLoginParamDto.getL2RMemberNo());
        queryLIM.setLhotelGroupCode(memberLoginParamDto.getLhotelGroupCode());
        QueryResult<L2RIpms2Member> l2RIpms2MemberResult = commonService.findAllEQ(L2RIpms2Member.class, queryLIM);
        L2RIpms2Member l2RIpms2Member = null;
        if (l2RIpms2MemberResult.getResultlist() != null && l2RIpms2MemberResult.getResultlist().size() > 0) {
            l2RIpms2Member = l2RIpms2MemberResult.getResultlist().get(0);
        }

        if (null != l2RIpms2Member) {
            this.unbindMemberInfo(memberLoginParamDto.getLhotelGroupCode(),
                    l2RIpms2Group.getHotelGroupId(),
                    l2RIpms2Member.getMemberId(),
                    BindMemberConstant._bindOpenType.LINK2ROOM,
                    memberLoginParamDto.getL2RMemberNo());

            try {
                this.unbindMemberInfo(memberLoginParamDto.getLhotelGroupCode(),
                        l2RIpms2Group.getHotelGroupId(),
                        l2RIpms2Member.getMemberId(),
                        BindMemberConstant._bindOpenType.WEIXIN,
                        memberLoginParamDto.getOpenid());
            } catch (Exception e) {
                //不做处理
                logger.error("解绑微信失败" + e.getMessage());
            }
            gcPmsMemberService.unbindL2RIpms2Member(l2RIpms2Member);
        }
    }

    @Override
    public com.link2room.soundwave.dto.result.base.PmsMemberPointDto getPmsMemberPoints(MemberLoginInsideParamDto memberLoginParamDto) throws Exception {
        com.link2room.soundwave.dto.result.base.PmsMemberPointDto pmsMemberPointDto = null;

        if (StringUtil.isNotBlank(memberLoginParamDto.getL2RMemberNo())) {
            L2RIpms2Member querylpm = new L2RIpms2Member();
            querylpm.setLhotelGroupCode(memberLoginParamDto.getLhotelGroupCode());
            querylpm.setL2RMemberNo(memberLoginParamDto.getL2RMemberNo());
            L2RIpms2Member l2RIpms2Member = commonService.findOneEQ(L2RIpms2Member.class, querylpm);
            if (null != l2RIpms2Member) {
                pmsMemberPointDto = this.getMemberPoint(memberLoginParamDto.getLhotelGroupCode(),
                        l2RIpms2Member.getHotelGroupId(),
                        l2RIpms2Member.getCardId());
            }
        }
        return pmsMemberPointDto;
    }

    @Override
    public QueryResult<PmsCouponDto> getPmsMemberCoupons(MemberLoginInsideParamDto memberLoginParamDto) throws Exception {
        QueryResult<PmsCouponDto> pmsMemberCouponsDtoQueryResult = null;
        L2RIpms2Member querylpm = new L2RIpms2Member();
        querylpm.setLhotelGroupCode(memberLoginParamDto.getLhotelGroupCode());
        querylpm.setL2RMemberNo(memberLoginParamDto.getL2RMemberNo());
        L2RIpms2Member l2RIpms2Member = commonService.findOneEQ(L2RIpms2Member.class, querylpm);
        if (null != l2RIpms2Member) {
            FindCouponDetailListByMemberIdRequest request = FindCouponDetailListByMemberIdRequest.newInstance();
            request.setHotelGroupId(l2RIpms2Member.getHotelGroupId());
            request.setMemberId(l2RIpms2Member.getMemberId());
            request.setPageNum(memberLoginParamDto.getCurrentPage());
            request.setPageSize(memberLoginParamDto.getPageSize());
            CouponWebResponse resp = GcPmsUtil.getGcClient(memberLoginParamDto.getLhotelGroupCode()).execute(request);
            if (resp.getResultCode() < 0) {
                throw new SoundwaveException(SoundwaveSysExceptionEnum.GCPMS_GET_MEMBER_COUPON_ERROR, resp.getResultMsg());
            } else {
                com.link2room.soundwave.dto.result.base.PmsMemberCouponsDto pmsMemberCouponsDto = ClassConverUtil.copyProperties(resp, com.link2room.soundwave.dto.result.base.PmsMemberCouponsDto.class);
                pmsMemberCouponsDtoQueryResult = new QueryResult<>();
                pmsMemberCouponsDtoQueryResult.setResultlist(pmsMemberCouponsDto.getListCouponDetailWebDtos());
                pmsMemberCouponsDtoQueryResult.setCurrentPage(memberLoginParamDto.getCurrentPage());
                pmsMemberCouponsDtoQueryResult.setTotalrecord(resp.getTotalRows());
            }
        }
        return pmsMemberCouponsDtoQueryResult;
    }

    @Override
    public void checkRegister(MemberLoginInsideParamDto memberLoginInsideParamDto) throws Exception {
        L2RIpms2Group l2RIpms2Group = new L2RIpms2Group();
        l2RIpms2Group.setLhotelGroupCode(memberLoginInsideParamDto.getLhotelGroupCode());
        l2RIpms2Group = commonService.findOneEQ(L2RIpms2Group.class, l2RIpms2Group);
        this.checkRegister(memberLoginInsideParamDto.getLhotelGroupCode(),
                l2RIpms2Group.getHotelGroupId(),
                memberLoginInsideParamDto.getMobile());
    }

    @Override
    public PmsMemberInfoDto newPMSMember(MemberLoginInsideParamDto memberLoginInsideParamDto) throws Exception {

        L2RIpms2Group l2RIpms2Group = new L2RIpms2Group();
        l2RIpms2Group.setLhotelGroupCode(memberLoginInsideParamDto.getLhotelGroupCode());
        l2RIpms2Group = commonService.findOneEQ(L2RIpms2Group.class, l2RIpms2Group);

        L2RIpms2Hotel l2RIpms2Hotel = new L2RIpms2Hotel();
        l2RIpms2Hotel.setLhotelGroupCode(memberLoginInsideParamDto.getLhotelGroupCode());
        l2RIpms2Hotel.setLhotelCode(memberLoginInsideParamDto.getLhotelCode());
        l2RIpms2Hotel = commonService.findOneEQ(L2RIpms2Hotel.class, l2RIpms2Hotel);
        Long hotelId = null;
        if (l2RIpms2Hotel != null) {
            hotelId = l2RIpms2Hotel.getHotelId();
        }
        PmsMemberInfoDto pmsMemberInfoDto = this.newPMSMember(memberLoginInsideParamDto.getLhotelGroupCode(),
                l2RIpms2Group.getHotelGroupId(), hotelId,
                memberLoginInsideParamDto.getMemberName(), memberLoginInsideParamDto.getMobile());
        if (null == pmsMemberInfoDto.getCardId()) {
            throw new SoundwaveException(SoundwaveSysExceptionEnum.REGISTER_ERROR);
        } else {
            this.saveL2RIpms2Member(memberLoginInsideParamDto.getLhotelGroupCode(),
                    memberLoginInsideParamDto.getL2RMemberNo(),
                    l2RIpms2Group.getHotelGroupId(), Long.parseLong(pmsMemberInfoDto.getPmsHotelCode()),
                    pmsMemberInfoDto);
        }
        return pmsMemberInfoDto;
    }

    @Override
    public void presentPoint(PointPresentUseInsideParamDto pointPresentUseInsideParamDto) throws Exception {
        L2RIpms2Group l2RIpms2Group = new L2RIpms2Group();
        l2RIpms2Group.setLhotelGroupCode(pointPresentUseInsideParamDto.getLhotelGroupCode());
        l2RIpms2Group = commonService.findOneEQ(L2RIpms2Group.class, l2RIpms2Group);

        L2RIpms2Member queryLIM = new L2RIpms2Member();
        queryLIM.setL2RMemberNo(pointPresentUseInsideParamDto.getL2rMemberNo());
        queryLIM.setLhotelGroupCode(pointPresentUseInsideParamDto.getLhotelGroupCode());
        QueryResult<L2RIpms2Member> l2RIpms2MemberResult = commonService.findAllEQ(L2RIpms2Member.class, queryLIM);
        L2RIpms2Member l2RIpms2Member = null;
        if (l2RIpms2MemberResult.getResultlist() != null && l2RIpms2MemberResult.getResultlist().size() > 0) {
            l2RIpms2Member = l2RIpms2MemberResult.getResultlist().get(0);
        }
        if (null != l2RIpms2Group && null != l2RIpms2Member) {
            this.presentMemberPoint(pointPresentUseInsideParamDto.getLhotelGroupCode(),
                    l2RIpms2Group.getHotelGroupId(),
                    l2RIpms2Member.getCardId(),
                    pointPresentUseInsideParamDto.getScene(),
                    pointPresentUseInsideParamDto.getRemark(),
                    pointPresentUseInsideParamDto.getPoint().toString()
            );
        }
    }

    @Override
    public void usePoint(PointPresentUseInsideParamDto pointPresentUseInsideParamDto) throws Exception {
        L2RIpms2Group l2RIpms2Group = new L2RIpms2Group();
        l2RIpms2Group.setLhotelGroupCode(pointPresentUseInsideParamDto.getLhotelGroupCode());
        l2RIpms2Group = commonService.findOneEQ(L2RIpms2Group.class, l2RIpms2Group);

        L2RIpms2Member queryLIM = new L2RIpms2Member();
        queryLIM.setL2RMemberNo(pointPresentUseInsideParamDto.getL2rMemberNo());
        queryLIM.setLhotelGroupCode(pointPresentUseInsideParamDto.getLhotelGroupCode());
        QueryResult<L2RIpms2Member> l2RIpms2MemberResult = commonService.findAllEQ(L2RIpms2Member.class, queryLIM);
        L2RIpms2Member l2RIpms2Member = null;
        if (l2RIpms2MemberResult.getResultlist() != null && l2RIpms2MemberResult.getResultlist().size() > 0) {
            l2RIpms2Member = l2RIpms2MemberResult.getResultlist().get(0);
        }
        if (null != l2RIpms2Group && null != l2RIpms2Member) {
            this.useMemberPoint(pointPresentUseInsideParamDto.getLhotelGroupCode(),
                    l2RIpms2Group.getHotelGroupId(),
                    l2RIpms2Member.getCardNo(),
                    pointPresentUseInsideParamDto.getScene(),
                    pointPresentUseInsideParamDto.getRemark(),
                    pointPresentUseInsideParamDto.getPoint().toString()
            );
        }
    }

    @Override
    List<com.link2room.soundwave.dto.result.base.PmsCardPointDto> getPmsMemberPointsDetail(MemberLoginInsideParamDto memberLoginParamDto) throws Exception {
        com.link2room.soundwave.dto.result.base.PmsMemberPointDto pmsMemberPointDto = null;

        if (StringUtil.isNotBlank(memberLoginParamDto.getL2RMemberNo())) {
            L2RIpms2Member querylpm = new L2RIpms2Member();
            querylpm.setLhotelGroupCode(memberLoginParamDto.getLhotelGroupCode());
            querylpm.setL2RMemberNo(memberLoginParamDto.getL2RMemberNo());
            L2RIpms2Member l2RIpms2Member = commonService.findOneEQ(L2RIpms2Member.class, querylpm);
            if (null != l2RIpms2Member) {
                pmsMemberPointDto = this.getMemberPoint(memberLoginParamDto.getLhotelGroupCode(),
                        l2RIpms2Member.getHotelGroupId(),
                        l2RIpms2Member.getCardId());
            }
        }
        return pmsMemberPointDto.getCardPointList();
    }

    @Override
    public PmsMemberInfoDto autoMember(MemberLoginInsideParamDto memberLoginInsideParamDto) throws Exception {
        PmsMemberInfoDto pmsMemberInfoDto = null;
        String lhotelGroupCode = memberLoginInsideParamDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is null.");
        String l2rMemberNo = memberLoginInsideParamDto.getL2RMemberNo();
        L2RAssert.stringBlank(l2rMemberNo, "[l2rMemberNo] is null.");
        String mobile = memberLoginInsideParamDto.getMobile();
        L2RAssert.stringBlank(mobile, "[mobile] is null.");
        String memberName = memberLoginInsideParamDto.getMemberName();
        L2RAssert.stringBlank(memberName, "[memberName] is null");
        String wxOpenid = memberLoginInsideParamDto.getOpenid();
        L2RIpms2Group l2RIpms2Group = new L2RIpms2Group();
        l2RIpms2Group.setLhotelGroupCode(memberLoginInsideParamDto.getLhotelGroupCode());
        l2RIpms2Group = commonService.findOneEQ(L2RIpms2Group.class, l2RIpms2Group);
        L2RIpms2Hotel l2RIpms2Hotel = null;
        if (StringUtil.isNotBlank(memberLoginInsideParamDto.getLhotelCode())) {
            L2RIpms2Hotel querylh = new L2RIpms2Hotel();
            querylh.setLhotelGroupCode(memberLoginInsideParamDto.getLhotelGroupCode());
            querylh.setLhotelCode(memberLoginInsideParamDto.getLhotelCode());
            l2RIpms2Hotel = commonService.findOneEQ(L2RIpms2Hotel.class, querylh);
        }
        Long hotelId = null;
        if (l2RIpms2Hotel != null) {
            hotelId = l2RIpms2Hotel.getHotelId();
        }
        if (l2RIpms2Group == null) {
            throw new SysException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE);
        }
        if (l2RIpms2Group.getHotelGroupId() == null) {
            throw new SysException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE, "hotel_gorup_id is null");
        }
        Long hotelGroupId = Long.valueOf(l2RIpms2Group.getHotelGroupId());

        Long memberId = this.matchingLogin(lhotelGroupCode, hotelGroupId, memberName, mobile);
        //pms匹配测试
        if (memberId != null) {
            _bindOnPMSmember(hotelGroupId, lhotelGroupCode, memberId, l2rMemberNo, wxOpenid);
        } else {

            _checkRegister(hotelGroupId, mobile, l2rMemberNo, lhotelGroupCode);
            //检查联房会员

            //注册
            _signUpPMSMember(hotelGroupId, hotelId, lhotelGroupCode, memberLoginInsideParamDto.getMobile(), l2rMemberNo, memberName, wxOpenid);
        }
        pmsMemberInfoDto = this.getMemberInfoOpen(memberLoginInsideParamDto.getLhotelGroupCode(),
                l2RIpms2Group.getHotelGroupId(),
                null,
                l2rMemberNo,
                BindMemberConstant._bindOpenType.LINK2ROOM
        );
        return pmsMemberInfoDto;
    }

    private void _checkRegister(Long hotelGorupId, String mobile, String l2rMemberNo, String lhotelGroupCode) throws Exception {
        this.checkRegister(lhotelGroupCode, hotelGorupId, mobile);
        L2RIpms2Member res = _selectL2Rpms2Member(lhotelGroupCode, l2rMemberNo);
        if (res != null) {
            throw new SysException(SoundwaveSysExceptionEnum.L2R_REGISTER_CHECK, "[" + lhotelGroupCode + "," + l2rMemberNo + "]l2r_ipms2_member重复");
        }

    }

    private L2RIpms2Member _getL2RIpms2Member(String lhotelGroupCode, String l2rMemberNo) throws Exception {
        L2RIpms2Member l2RIpms2Member = _selectL2Rpms2Member(lhotelGroupCode, l2rMemberNo);
        if (null == l2RIpms2Member) {
            l2RIpms2Member = new L2RIpms2Member();
            l2RIpms2Member.setLhotelGroupCode(lhotelGroupCode);
            l2RIpms2Member.setL2RMemberNo(l2rMemberNo);
        }
        return l2RIpms2Member;
    }

    private L2RIpms2Member _selectL2Rpms2Member(String lhotelGroupCode, String l2rMemberNo) throws Exception {
        L2RIpms2Member l2RIpms2Member = null;
        if (StringUtil.isNotBlank(l2rMemberNo)) {
            L2RIpms2Member querylpm = new L2RIpms2Member();
            querylpm.setLhotelGroupCode(lhotelGroupCode);
            querylpm.setL2RMemberNo(l2rMemberNo);
            l2RIpms2Member = commonService.findOneEQ(L2RIpms2Member.class, querylpm);
        }
        return l2RIpms2Member;
    }

    private void _bindOnPMSmember(Long hotelGroupId, String lhotelGroupCode, Long pmsMemberId, String l2rMemberNo, String wxOpenid) throws Exception {
        this.bindMemberInfo(lhotelGroupCode,
                hotelGroupId,
                pmsMemberId,
                BindMemberConstant._bindOpenType.LINK2ROOM,
                l2rMemberNo,
                "");

        PmsMemberInfoDto pmsMemberInfoDto = this.getMemberInfoOpen(lhotelGroupCode, hotelGroupId, null, l2rMemberNo, BindMemberConstant._bindOpenType.LINK2ROOM);
        if (pmsMemberInfoDto != null) {
            L2RIpms2Member l2RIpms2Member = _getL2RIpms2Member(lhotelGroupCode, l2rMemberNo);
            Long tempId = l2RIpms2Member.getId();
            ClassConverUtil.copyProperties(pmsMemberInfoDto, l2RIpms2Member);
            l2RIpms2Member.setId(tempId);
            l2RIpms2Member.setCardId(Long.parseLong(pmsMemberInfoDto.getCardId()));
            l2RIpms2Member.setHotelGroupId(hotelGroupId);
            if (StringUtil.isNotBlank(pmsMemberInfoDto.getPmsHotelCode())) {
                l2RIpms2Member.setHotelId(Long.parseLong(pmsMemberInfoDto.getPmsHotelCode()));
            }
            l2RIpms2Member.setL2RMemberNo(l2rMemberNo);
            l2RIpms2Member.setLhotelGroupCode(lhotelGroupCode);
            commonService.saveOrUpdate(l2RIpms2Member);

            this.bindMemberInfo(lhotelGroupCode,
                    hotelGroupId,
                    pmsMemberId,
                    BindMemberConstant._bindOpenType.WEIXIN,
                    wxOpenid,
                    "");
        }
    }

    private void _signUpPMSMember(Long hotelGroupId, Long hotelId, String lhotelGroupCode, String mobile, String l2rMemberNo, String memberName, String wxOpenid) throws Exception {

        PmsMemberInfoDto pmsMemberInfoDto = this.newPMSMember(lhotelGroupCode, hotelGroupId, hotelId, memberName, mobile);

        if (null == pmsMemberInfoDto) {
            throw new SoundwaveException(SoundwaveSysExceptionEnum.REGISTER_ERROR);
        } else if (null == pmsMemberInfoDto.getCardId()) {
            throw new SoundwaveException(SoundwaveSysExceptionEnum.REGISTER_ERROR);
        } else {
            L2RIpms2Member l2RIpms2Member = this.saveL2RIpms2Member(lhotelGroupCode, l2rMemberNo, hotelGroupId, Long.parseLong(pmsMemberInfoDto.getPmsHotelCode()), pmsMemberInfoDto);

            //先测试L2R是否已绑定
            PmsMemberInfoDto pmsMemberInfoDtoL2R = this.getMemberInfoOpen(lhotelGroupCode, hotelGroupId, null, l2rMemberNo, BindMemberConstant._bindOpenType.LINK2ROOM);

            if (pmsMemberInfoDtoL2R != null && null == pmsMemberInfoDtoL2R.getCardId()) {
                this.bindMemberInfo(lhotelGroupCode,
                        hotelGroupId,
                        pmsMemberInfoDto.getMemberId(),
                        BindMemberConstant._bindOpenType.LINK2ROOM,
                        l2rMemberNo, "");
            } else if (pmsMemberInfoDtoL2R != null && !pmsMemberInfoDto.getCardId().equals(pmsMemberInfoDtoL2R.getCardId())) {
                this.unbindMemberInfo(lhotelGroupCode,
                        hotelGroupId,
                        pmsMemberInfoDtoL2R.getMemberId(),
                        BindMemberConstant._bindOpenType.LINK2ROOM,
                        l2rMemberNo);

                this.bindMemberInfo(lhotelGroupCode,
                        hotelGroupId,
                        pmsMemberInfoDto.getMemberId(),
                        BindMemberConstant._bindOpenType.LINK2ROOM,
                        l2rMemberNo, "");
            }


            pmsMemberInfoDto = this.getMemberInfoOpen(lhotelGroupCode, hotelGroupId, null, l2rMemberNo, BindMemberConstant._bindOpenType.LINK2ROOM);

            Long tempId = l2RIpms2Member.getId();
            if (pmsMemberInfoDto != null) {
                ClassConverUtil.copyProperties(pmsMemberInfoDto, l2RIpms2Member);
                l2RIpms2Member.setCardId(Long.parseLong(pmsMemberInfoDto.getCardId()));
            }
            l2RIpms2Member.setId(tempId);
            if (StringUtil.isNotBlank(pmsMemberInfoDto.getPmsHotelCode())) {
                l2RIpms2Member.setHotelId(Long.parseLong(pmsMemberInfoDto.getPmsHotelCode()));
            }
            l2RIpms2Member.setHotelGroupId(hotelGroupId);
            l2RIpms2Member.setL2RMemberNo(l2rMemberNo);
            l2RIpms2Member.setLhotelGroupCode(lhotelGroupCode);
            commonService.saveOrUpdate(l2RIpms2Member);

            //再测试微信是否绑定
            PmsMemberInfoDto pmsMemberInfoDtoWX = this.getMemberInfoOpen(lhotelGroupCode, hotelGroupId, null, wxOpenid, BindMemberConstant._bindOpenType.WEIXIN);


            if (pmsMemberInfoDtoWX != null && null == pmsMemberInfoDtoWX.getCardId()) {
                this.unbindMemberInfo(lhotelGroupCode,
                        hotelGroupId,
                        pmsMemberInfoDto.getMemberId(),
                        BindMemberConstant._bindOpenType.WEIXIN,
                        wxOpenid);
            } else if (pmsMemberInfoDtoWX != null && !pmsMemberInfoDto.getCardId().equals(pmsMemberInfoDtoWX.getCardId())) {
                this.unbindMemberInfo(lhotelGroupCode,
                        hotelGroupId,
                        pmsMemberInfoDto.getMemberId(),
                        BindMemberConstant._bindOpenType.WEIXIN,
                        wxOpenid);

                this.bindMemberInfo(lhotelGroupCode,
                        hotelGroupId,
                        pmsMemberInfoDto.getMemberId(),
                        BindMemberConstant._bindOpenType.WEIXIN,
                        wxOpenid, "");
            }
        }


    }

    @Override
    public PmsMemberInfoDto syncMember(MemberLoginInsideParamDto memberLoginInsideParamDto) throws Exception {
        L2RIpms2Group l2RIpms2Group = new L2RIpms2Group();
        l2RIpms2Group.setLhotelGroupCode(memberLoginInsideParamDto.getLhotelGroupCode());
        l2RIpms2Group = commonService.findOneEQ(L2RIpms2Group.class, l2RIpms2Group);

        PmsMemberInfoDto pmsMemberInfoDto = this.getMemberInfoOpen(memberLoginInsideParamDto.getLhotelGroupCode(), l2RIpms2Group.getHotelGroupId(), null, memberLoginInsideParamDto.getL2RMemberNo(), BindMemberConstant._bindOpenType.LINK2ROOM);


        L2RIpms2Member l2RIpms2Member = null;
        if (StringUtil.isNotBlank(memberLoginInsideParamDto.getL2RMemberNo())) {
            L2RIpms2Member querylpm = new L2RIpms2Member();
            querylpm.setLhotelGroupCode(memberLoginInsideParamDto.getLhotelGroupCode());
            querylpm.setL2RMemberNo(memberLoginInsideParamDto.getL2RMemberNo());
            QueryResult<L2RIpms2Member> l2RIpms2MemberResult = commonService.findAllEQ(L2RIpms2Member.class, querylpm);
            if (l2RIpms2MemberResult.getResultlist() != null && l2RIpms2MemberResult.getResultlist().size() > 0) {
                l2RIpms2Member = l2RIpms2MemberResult.getResultlist().get(0);
            }
        }
        if (null == l2RIpms2Member) {
            l2RIpms2Member = new L2RIpms2Member();
        }

        Long tempId = l2RIpms2Member.getId();
        if (tempId != null && pmsMemberInfoDto != null) {
            ClassConverUtil.copyProperties(pmsMemberInfoDto, l2RIpms2Member);
            l2RIpms2Member.setId(tempId);
            try {
                l2RIpms2Member.setCardId(Long.parseLong(pmsMemberInfoDto.getCardId()));
                l2RIpms2Member.setHotelGroupId(l2RIpms2Group.getHotelGroupId());
                if (StringUtil.isNotBlank(pmsMemberInfoDto.getPmsHotelCode())) {
                    l2RIpms2Member.setHotelId(Long.parseLong(pmsMemberInfoDto.getPmsHotelCode()));
                }
                l2RIpms2Member.setL2RMemberNo(memberLoginInsideParamDto.getL2RMemberNo());
                l2RIpms2Member.setLhotelGroupCode(l2RIpms2Group.getLhotelGroupCode());
                commonService.updateById(l2RIpms2Member);
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }
        return pmsMemberInfoDto;
    }

    @Override
    public PmsCardBalanceDto getCardBalanceInfo(MemberLoginInsideParamDto memberLoginInsideParamDto) throws Exception {
        PmsCardBalanceDto pmsCardBalanceDto = null;
        L2RIpms2Group l2RIpms2Group = new L2RIpms2Group();
        l2RIpms2Group.setLhotelGroupCode(memberLoginInsideParamDto.getLhotelGroupCode());
        l2RIpms2Group = commonService.findOneEQ(L2RIpms2Group.class, l2RIpms2Group);


        L2RIpms2Member l2RIpms2Member = new L2RIpms2Member();
        l2RIpms2Member.setLhotelGroupCode(memberLoginInsideParamDto.getLhotelGroupCode());
        l2RIpms2Member.setL2RMemberNo(memberLoginInsideParamDto.getL2RMemberNo());
        l2RIpms2Member = commonService.findOneEQ(L2RIpms2Member.class, l2RIpms2Member);
        if (null != l2RIpms2Member) {
            pmsCardBalanceDto = this.getCardBalanceInfo(memberLoginInsideParamDto.getLhotelGroupCode(),
                    l2RIpms2Group.getHotelGroupId(),
                    l2RIpms2Member.getCardId(),
                    "");
        }
        return pmsCardBalanceDto;
    }

    @Override
    public QueryResult<PmsCouponDto> getUsableCoupons(UsableCouopnsInsideParamDto usableCouopnsInsideParamDto) throws Exception {
        QueryResult<PmsCouponDto> pmsUsableCouponDtoQueryResult = new QueryResult<>();
        L2RIpms2Hotel l2RIpms2Hotel = new L2RIpms2Hotel();
        l2RIpms2Hotel.setLhotelGroupCode(usableCouopnsInsideParamDto.getLhotelGroupCode());
        l2RIpms2Hotel.setLhotelCode(usableCouopnsInsideParamDto.getLhotelCode());
        l2RIpms2Hotel = commonService.findOneEQ(L2RIpms2Hotel.class, l2RIpms2Hotel);

        L2RIpms2Member l2RIpms2Member = null;
        if (StringUtil.isNotBlank(usableCouopnsInsideParamDto.getL2RMemberNo())) {
            L2RIpms2Member querylpm = new L2RIpms2Member();
            querylpm.setLhotelGroupCode(usableCouopnsInsideParamDto.getLhotelGroupCode());
            querylpm.setL2RMemberNo(usableCouopnsInsideParamDto.getL2RMemberNo());
            l2RIpms2Member = commonService.findOneEQ(L2RIpms2Member.class, querylpm);
        }

        L2RIpms2Rmtype l2RIpms2Rmtype = new L2RIpms2Rmtype();
        l2RIpms2Rmtype.setLhotelGroupCode(usableCouopnsInsideParamDto.getLhotelGroupCode());
        l2RIpms2Rmtype.setLhotelCode(usableCouopnsInsideParamDto.getLhotelCode());
        l2RIpms2Rmtype.setL2RRmtype(usableCouopnsInsideParamDto.getRmtype());
        l2RIpms2Rmtype = commonService.findOneEQ(L2RIpms2Rmtype.class, l2RIpms2Rmtype);


        if (null != l2RIpms2Member && null != l2RIpms2Rmtype) {
            FindCouponDetailListByCondiRequest request = FindCouponDetailListByCondiRequest.newInstance();
            request.setHotelGroupId(l2RIpms2Hotel.getHotelGroupId());
            request.setHotelId(Long.parseLong(l2RIpms2Rmtype.getHotelId()));
            request.setArr(usableCouopnsInsideParamDto.getArr());
            request.setRmtype(l2RIpms2Rmtype.getPmsRmtype());
            request.setMemberId(l2RIpms2Member.getMemberId());
            request.setUseDate(usableCouopnsInsideParamDto.getArr()
            );
            request.setRateCode(usableCouopnsInsideParamDto.getRateCode());
            request.setCouponType("DF");
            CouponWebResponse couponWebResponse = GcPmsUtil.getGcClient(usableCouopnsInsideParamDto.getLhotelGroupCode()).execute(request);
            if (couponWebResponse.getResultCode() < 0) {
                LogUtil.logger.error("绿云券查询异常：" + JacksonUtil.beanToJson(couponWebResponse));
            } else {
                pmsUsableCouponDtoQueryResult.setResultlist(ClassConverUtil.copyPropertiesList(couponWebResponse.getListCouponDetailWebDtos(), PmsCouponDto.class));
                pmsUsableCouponDtoQueryResult.setCurrentPage(1);
                pmsUsableCouponDtoQueryResult.setTotalrecord(couponWebResponse.getListCouponDetailWebDtos().size());
            }
        }
        return pmsUsableCouponDtoQueryResult;
    }

    @Override
    PmsMemberInfoDto getCurrentUserPMSMemberInfo(BaseL2rParamDto paramDto) throws Exception {
        MemberSessionDto session = ApiThreadLocal.getMemberSessionThreadLocal();
        if (DefaultValue.T.equals(session.getIsMember())) {
            L2RIpms2Member find = new L2RIpms2Member();
            find.setL2RMemberNo(session.getMemberNo());
            L2RIpms2Member qres = commonService.findOneEQ(L2RIpms2Member.class, find);
            if (qres == null) {
                return null;
            } else {
                return ClassConverUtil.copyProperties(qres, PmsMemberInfoDto.class);
            }
        } else {
            return null;
        }
    }

    /**
     * 绑定PMS会员信息
     *
     * @param lhotelGroupCode
     * @param hotelGroupId
     * @param memberId
     * @param openIdType
     * @param openIdUserId
     * @return
     * @throws Exception
     */
    private PmsResponse bindMemberInfo(String lhotelGroupCode,
                                       Long hotelGroupId,
                                       Long memberId,
                                       String openIdType,
                                       String openIdUserId,
                                       String cardNo) throws Exception {
        BindOpenIdRequest request = BindOpenIdRequest.newInstance();
        request.setHotelGroupId(hotelGroupId);
        request.setMemberId(memberId);
        request.setOpenIdType(openIdType);
        request.setOpenIdUserId(openIdUserId);
        request.setCardNo(cardNo);
        PmsResponse resp = GcPmsUtil.getGcClient(lhotelGroupCode).execute(request);
        if (resp.getResultCode() < 0) {
            throw new SysException(SoundwaveSysExceptionEnum.PMSMEMBER_NOT_EXIT, "[openidType]" + openIdType + resp.getResultMsg());
        }
        return resp;
    }

    /**
     * 绑定PMS会员信息
     *
     * @param lhotelGroupCode
     * @param hotelGroupId
     * @param memberId
     * @param openIdType
     * @param openIdUserId
     * @return
     * @throws Exception
     */
    private PmsResponse unbindMemberInfo(String lhotelGroupCode,
                                         Long hotelGroupId,
                                         Long memberId,
                                         String openIdType,
                                         String openIdUserId
    ) throws Exception {
        RemoveOpenIdRequest request = RemoveOpenIdRequest.newInstance();
        request.setHotelGroupId(hotelGroupId);
        request.setMemberId(memberId);
        request.setOpenIdType(openIdType);
        request.setOpenIdUserId(openIdUserId);
        PmsResponse resp = GcPmsUtil.getGcClient(lhotelGroupCode).execute(request);
        if (resp.getResultCode() < 0) {
            throw new SysException(SoundwaveSysExceptionEnum.PMSMEMBER_NOT_EXIT, "[openidType]" + openIdType + resp.getResultMsg());
        }
        return resp;
    }

    private PmsMemberInfoDto getMemberInfo(String lhotelGroupCode,
                                                                                   Long hotelGroupId,
                                                                                   String loginId,
                                                                                   String loginPassword
    ) throws Exception {
        MemberLoginRequest request = MemberLoginRequest.newInstance();
        request.setHotelGroupId(hotelGroupId);
        request.setLoginId(loginId);
        request.setLoginPassword(loginPassword);
        MemberCardResponse response = GcPmsUtil.getGcClient(lhotelGroupCode).execute(request);
        if (response.getResultCode() < 0) {
            //TODO 临时retrun 空PmsMemberInfoDto
            return new PmsMemberInfoDto();
        } else {
            PmsMemberInfoDto pmsMemberInfoDto = ClassConverUtil.copyProperties(response, PmsMemberInfoDto.class);
            if (response.getHotelId() != null) {
                pmsMemberInfoDto.setPmsHotelCode(response.getHotelId().toString());
            }
            return pmsMemberInfoDto;
        }
    }

    private PmsMemberInfoDto getMemberInfoOpen(String lhotelGroupCode,
                                                                                       Long hotelGroupId,
                                                                                       Long hotelId,
                                                                                       String openId,
                                                                                       String openType
    ) throws Exception {
        MemberLoginOpenRequest request = MemberLoginOpenRequest.newInstance();
        request.setHotelGroupId(hotelGroupId);
        request.setHotelId(hotelId);
        request.setOpenId(openId);
        request.setOpenType(openType);
        MemberCardResponse response = GcPmsUtil.getGcClient(lhotelGroupCode).execute(request);
        if (response.getResultCode() < 0) {
            return null;
        } else {
            PmsMemberInfoDto pmsMemberInfoDto = ClassConverUtil.copyProperties(response, PmsMemberInfoDto.class);
            pmsMemberInfoDto.setBirth(DateUtil.getNewFormatDateString(response.getBirth()));
            if (response.getHotelId() != null) {
                pmsMemberInfoDto.setPmsHotelCode(response.getHotelId().toString());
            }
            return pmsMemberInfoDto;
        }
    }

    private L2RIpms2Member saveL2RIpms2Member(String lhotelGroupCode,
                                              String l2rMemberNo,
                                              Long hotelGroupId,
                                              Long hotelId, PmsMemberInfoDto pmsMemberInfoDto) throws Exception {
        L2RIpms2Member l2RIpms2Member = new L2RIpms2Member();
        l2RIpms2Member.setLhotelGroupCode(lhotelGroupCode);
        l2RIpms2Member.setL2RMemberNo(l2rMemberNo);
        l2RIpms2Member.setHotelId(hotelId);
        l2RIpms2Member.setMobile(pmsMemberInfoDto.getMobile());
        if (pmsMemberInfoDto.getCardId() != null) {
            l2RIpms2Member.setCardId(Long.parseLong(pmsMemberInfoDto.getCardId()));
        } else {
            l2RIpms2Member.setCardId(0L);
        }
        l2RIpms2Member.setCardNo(pmsMemberInfoDto.getCardNo());
        l2RIpms2Member.setCardLevel(pmsMemberInfoDto.getCardLevel());
        l2RIpms2Member.setCardType(pmsMemberInfoDto.getCardType());
        l2RIpms2Member.setHotelGroupId(hotelGroupId);
        l2RIpms2Member.setMemberId(pmsMemberInfoDto.getMemberId());
        l2RIpms2Member.setName(pmsMemberInfoDto.getName());
        l2RIpms2Member.setSta("I");
        l2RIpms2Member.setPhone(pmsMemberInfoDto.getMobile());
        l2RIpms2Member.setIdType(pmsMemberInfoDto.getIdType());
        l2RIpms2Member.setIdNo(pmsMemberInfoDto.getIdNo());
        l2RIpms2Member.setStreet(pmsMemberInfoDto.getStreet());
        l2RIpms2Member.setBirth(pmsMemberInfoDto.getBirth());
        l2RIpms2Member.setEmail(pmsMemberInfoDto.getEmail());
        l2RIpms2Member.setSex(pmsMemberInfoDto.getSex());
        commonService.save(l2RIpms2Member);
        return l2RIpms2Member;
    }

    private void mergeL2RIpms2Member(L2RIpms2Member l2RIpms2Member, PmsMemberInfoDto pmsMemberInfoDto) throws Exception {
        l2RIpms2Member.setName(pmsMemberInfoDto.getName());
        l2RIpms2Member.setMobile(pmsMemberInfoDto.getMobile());
        if (pmsMemberInfoDto.getCardId() != null) {
            l2RIpms2Member.setCardId(Long.parseLong(pmsMemberInfoDto.getCardId()));
        } else {
            l2RIpms2Member.setCardId(0L);
        }
        l2RIpms2Member.setCardNo(pmsMemberInfoDto.getCardNo());
        l2RIpms2Member.setCardLevel(pmsMemberInfoDto.getCardLevel());
        l2RIpms2Member.setCardType(pmsMemberInfoDto.getCardType());

        l2RIpms2Member.setName(pmsMemberInfoDto.getName());
        l2RIpms2Member.setSta("I");
        l2RIpms2Member.setPhone(pmsMemberInfoDto.getMobile());
        l2RIpms2Member.setIdType(pmsMemberInfoDto.getIdType());
        l2RIpms2Member.setIdNo(pmsMemberInfoDto.getIdNo());
        l2RIpms2Member.setStreet(pmsMemberInfoDto.getStreet());
        l2RIpms2Member.setBirth(pmsMemberInfoDto.getBirth());
        l2RIpms2Member.setEmail(pmsMemberInfoDto.getEmail());
        l2RIpms2Member.setSex(pmsMemberInfoDto.getSex());
        commonService.merge(l2RIpms2Member);
    }

    private com.link2room.soundwave.dto.result.base.PmsMemberPointDto getMemberPoint(String lhotelGroupCode,
                                                                                     Long hotelGroupId,
                                                                                     Long cardId
    ) throws Exception {
        GetPointListRequest request = GetPointListRequest.newInstance();
        request.setHotelGroupId(hotelGroupId);
        request.setCardId(cardId);
        PointListWebResponse resp = GcPmsUtil.getGcClient(lhotelGroupCode).execute(request);
        if (resp.getResultCode() < 0) {
            throw new SysException(SoundwaveSysExceptionEnum.GCPMS_MEMBER_POINT_NOT_EXIT, "[cardId]" + cardId + resp.getResultMsg());
        } else {
            com.link2room.soundwave.dto.result.base.PmsMemberPointDto pmsMemberPointDto = ClassConverUtil.copyProperties(resp, com.link2room.soundwave.dto.result.base.PmsMemberPointDto.class);
            pmsMemberPointDto.setPointBalance(new BigDecimal(resp.getPointBalance()));
            pmsMemberPointDto.setPointCharge(new BigDecimal(resp.getPointCharge()));
            pmsMemberPointDto.setPointPromotionTan(new BigDecimal(resp.getPointPromotionTan()));
            List<com.link2room.soundwave.dto.result.base.PmsCardPointDto> pmsCardPointDtoList = null;
            if (resp.getCardPointList() != null && resp.getCardPointList().size() > 0) {
                pmsCardPointDtoList = new ArrayList<>();
                for (PointWebDto pointWebDto : resp.getCardPointList()) {
                    com.link2room.soundwave.dto.result.base.PmsCardPointDto pmsCardPointDto = new com.link2room.soundwave.dto.result.base.PmsCardPointDto();
                    pmsCardPointDto.setCardNo(pointWebDto.getCardNo().toString());
                    pmsCardPointDto.setInvalidDate(pointWebDto.getInvalidDate());
                    pmsCardPointDto.setPoint(new BigDecimal(pointWebDto.getPoint()));
                    pmsCardPointDto.setPointGenDate(pointWebDto.getPointGenDate());
                    pmsCardPointDto.setShowCardNo(pointWebDto.getCardNo().toString());
                    pmsCardPointDto.datetimeToString();
                    pmsCardPointDtoList.add(pmsCardPointDto);
                }
                pmsMemberPointDto.setCardPointList(pmsCardPointDtoList);
            }
            return pmsMemberPointDto;
        }
    }

    private void checkRegister(String lhotelGroupCode, Long hotelGorupId, String mobile) throws Exception {
        CheckDoubleRequest request = CheckDoubleRequest.newInstance();
        request.setHotelGroupId(hotelGorupId);
        request.setMobile(mobile);
        PmsResponse resp = GcPmsUtil.getGcClient(lhotelGroupCode).execute(request);
        if (resp.getResultCode() < 0) {
            throw new SysException(SoundwaveSysExceptionEnum.PMS_REGISTER_CHECK, resp.getResultMsg());
        }
    }

    private PmsMemberInfoDto newPMSMember(String lhotelGroupCode, Long hotelGorupId, Long hotelId, String memberName, String mobile) throws Exception {
        RegisterMemberCardWithOutVerifyRequest request = RegisterMemberCardWithOutVerifyRequest.newInstance();
        request.setHotelGroupId(hotelGorupId);
        if (hotelId != null) {
            request.setHotelId(hotelId);
        }
        request.setName(memberName);
        request.setMobile(mobile);
        //TODO cardType需动态
        request.setCardType("NY");
        request.setCardLevel("0");
        request.setCardSales("");
        request.setCardSrc("");
        request.setCompanyId("");
        request.setIdNo("");
        request.setSex("");
        request.setEmail("");
        request.setPassword("888888");
        MemberCardResponse response = GcPmsUtil.getGcClient(lhotelGroupCode).execute(request);
        if (response.getResultCode() < 0) {
            throw new SoundwaveException(SoundwaveSysExceptionEnum.REGISTER_ERROR, response.getResultMsg());
        } else {
            PmsMemberInfoDto pmsMemberInfoDto = new PmsMemberInfoDto();
            ClassConverUtil.copyProperties(response, pmsMemberInfoDto);
            if (response.getHotelId() != null) {
                pmsMemberInfoDto.setPmsHotelCode(response.getHotelId().toString());
            }
            return pmsMemberInfoDto;
        }
    }

    private Long matchingLogin(String lhotelGroupCode, Long hotelGroupId, String memberName, String mobile) throws Exception {
        GetMemberIdByMobielAndNameRequest request = GetMemberIdByMobielAndNameRequest.newInstance();
        request.setHotelGroupId(hotelGroupId);
        request.setMobile(mobile);
        request.setName(memberName);
        GetMemberIdByMobielAndNameResponse resp = GcPmsUtil.getGcClient(lhotelGroupCode).execute(request);
        if (resp.getResultCode() < 0) {
            LogUtil.logger.error(ExceptionUtil.getExceptionDetail(new SoundwaveException(SoundwaveSysExceptionEnum.PMSMEMBER_NOT_MATCH, resp.getResultMsg())));
            return null;
        } else {
            return resp.getMemberId();
        }
    }

    private void presentMemberPoint(String lhotelGroupCode,
                                    Long hotelGroupId,
                                    Long cardId,
                                    String scene,
                                    String remark,
                                    String point
    ) throws Exception {
        PointPayByInterfaceRequest request = PointPayByInterfaceRequest.newInstance();
        request.setHotelGroupId(hotelGroupId);
        request.setCardId(cardId);
        request.setPoint(point.toString());
        if (StringUtil.isNotBlank(scene)) {
            request.setPromotionTag(scene);
        } else {
            request.setPromotionTag("");
        }
        if (StringUtil.isNotBlank(remark)) {
            request.setRemark(remark);
        } else {
            request.setRemark("");
        }
        PointPayByInterfaceResponse resp = GcPmsUtil.getGcClient(lhotelGroupCode).execute(request);
        if (resp.getResultCode() != 0) {
            throw new SoundwaveException(SoundwaveSysExceptionEnum.GCPMS_PAY_POINT_ERROR, resp.getResultMsg());
        }
    }

    private void useMemberPoint(String lhotelGroupCode,
                                Long hotelGroupId,
                                String cardNo,
                                String scene,
                                String remark,
                                String point
    ) throws Exception {
        PointApplyByInterfaceRequest request = PointApplyByInterfaceRequest.newInstance();
        request.setCardNo(cardNo);
        request.setPoint(point);
        request.setHotelGroupId(hotelGroupId);
        if (StringUtil.isNotBlank(scene)) {
            request.setPromotionTag(scene);
        } else {
            request.setPromotionTag("");
        }
        if (StringUtil.isNotBlank(remark)) {
            request.setRemark(remark);
        } else {
            request.setRemark("");
        }
        PointApplyByInterfaceResponse resp = GcPmsUtil.getGcClient(lhotelGroupCode).execute(request);
        if (resp.getResultCode() != 0) {
            throw new SoundwaveException(SoundwaveSysExceptionEnum.GCPMS_APPLY_POINT_ERROR, resp.getResultMsg());
        }
    }

    private PmsCardBalanceDto getCardBalanceInfo(String lhotelGroupCode,
                                                                                         Long hotelGroupId,
                                                                                         Long cardId,
                                                                                         String tag
    ) throws Exception {
        GetCardBalanceInfoRequest request = GetCardBalanceInfoRequest.newInstance();
        request.setHotelGroupId(hotelGroupId);
        request.setCardId(cardId.toString());
        if (StringUtil.isNotBlank(tag)) {
            request.setTag(tag);
        } else {
            request.setTag("");
        }
        GetCardBalanceInfoResponse resp = GcPmsUtil.getGcClient(lhotelGroupCode).execute(request);
        if (resp.getResultCode() < 0) {
            throw new SoundwaveException(SoundwaveSysExceptionEnum.GCPMS_CARD_BALANCE_ERROR, resp.getResultMsg());
        } else {
            PmsCardBalanceDto pmsCardBalanceDto = new PmsCardBalanceDto();
            pmsCardBalanceDto.setPointBalance(new BigDecimal(resp.getResult().getPointBalance()));
            pmsCardBalanceDto.setAccountBalance(new BigDecimal(resp.getResult().getAccountBalance()));
            pmsCardBalanceDto.setAccountBalanceUse(new BigDecimal(resp.getResult().getAccountBalanceUse()));
            return pmsCardBalanceDto;
        }
    }

    @Override
    String onlineSaleCoupon(UsableCouopnsInsideParamDto usableCouopnsInsideParamDto) throws Exception {

        L2RIpms2Group l2RIpms2Group = new L2RIpms2Group();
        l2RIpms2Group.setLhotelGroupCode(usableCouopnsInsideParamDto.getLhotelGroupCode());
        l2RIpms2Group = commonService.findOneEQ(L2RIpms2Group.class, l2RIpms2Group);

        L2RIpms2Member l2RIpms2Member = new L2RIpms2Member();
        l2RIpms2Member.setLhotelGroupCode(usableCouopnsInsideParamDto.getLhotelGroupCode());
        l2RIpms2Member.setL2RMemberNo(usableCouopnsInsideParamDto.getL2RMemberNo());
        l2RIpms2Member = commonService.findOneEQ(L2RIpms2Member.class, l2RIpms2Member);
        if (null != l2RIpms2Member) {

            OnlineSalesMemberCouponRequest request = OnlineSalesMemberCouponRequest.newInstance();
            request.setHotelGroupId(l2RIpms2Group.getHotelGroupId());
            if (StringUtil.isNotBlank(usableCouopnsInsideParamDto.getPmsCouponCode())) {
                request.setCouponCode(usableCouopnsInsideParamDto.getPmsCouponCode());
            } else {
                throw new SoundwaveException(SoundwaveSysExceptionEnum.SALE_COUPON_CODE_IS_MISSING);
            }
            request.setMemberId(l2RIpms2Member.getMemberId());
            request.setNumber(usableCouopnsInsideParamDto.getSaleNum().toString());
            String startDatetime=usableCouopnsInsideParamDto.getStartDatetime();
            String endDatetime=usableCouopnsInsideParamDto.getEndDatetime();
            if(StringUtil.isBlank(startDatetime)){
                Date now=new Date();
                startDatetime=DateUtil.getNewFormatDateString(now);
                endDatetime=DateUtil.getNewFormatDateString(DateUtil.addDays(now,30L));
            }
            request.setStartDate(startDatetime);
            request.setEndDate(endDatetime);
            OnlineSalesCouponResponse resp = GcPmsUtil.getGcClient(usableCouopnsInsideParamDto.getLhotelGroupCode()).execute(request);
            if (resp.getResultCode() != 0) {
                throw new SoundwaveException(GCPMS_SALE_COUPON_ERROR, JacksonUtil.beanToJson(resp));
            }
            return null;
        } else {
            throw new SoundwaveException(SoundwaveSysExceptionEnum.PMSMEMBER_NOT_MATCH);
        }
    }

    @Override
    PmsMemberInfoDto upgradePmsMemberLevel(MemberUpgradeInsideParamDto memberUpgradeInsideParamDto) throws Exception {
        return null;
    }

    @Override
    QueryResult<PmsPackageDto> listPmsPackage(BaseParamDto baseParamDto) throws Exception {
        return null;
    }

    @Override
    PmsPackageDto getPmsPackage(BaseParamDto baseParamDto) throws Exception {
        return null;
    }

    @Override
    void exchangePackage(MemberPackageInsideParamDto memberPackageInsideParamDto) throws Exception {

    }

	@Override
	public void coupenCodeCollarCoupenHandler(MemberCoupenCodeCollarCoupenInsideParamDto memberCouponsCodeInsideParamDto)
			throws Exception {
		
	}
}
